package com.peanut.youfupay.service.other;

import com.alibaba.fastjson.JSON;
import com.peanut.youfupay.entity.request.BalanceQuery;
import com.peanut.youfupay.entity.request.RequestMessage;
import com.peanut.youfupay.entity.response.BalanceQueryResult;
import com.peanut.youfupay.entity.response.ResponseMessage;
import com.peanut.youfupay.service.BaseService;
import com.peanut.youfupay.util.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.peanut.youfupay.constant.MerInfoConstant.*;
/**
 * 商户签约个人账户剩余可下发额度查询接口
 *
 * @Author :  peanut
 * @Created : 2020/11/9 上午9:31
 */
public class BalanceQueryService extends BaseService {


    public static void main(String[] args) throws Exception {
        RequestMessage rm = new RequestMessage();
        rm.setReqId("SohoBalanceQuery" + System.currentTimeMillis());
        //接口编码
        rm.setFunCode("6005");
        //企业商户编码
        rm.setMerId("89900000222116027420");
        //版本信息
        rm.setVersion("V1.0");
        String json = toJson();
        byte[] bs = DESUtils.encrypt(json.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        //请求数据
        rm.setReqData(reqDataEncrypt);
        //签名
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));
        String reqStr = JsonUtils.toJson(rm);
        System.out.println("请求报文：" + reqStr);

        Map<String, Object> map = new HashMap(2);
        map.put("reqJson", reqStr);
        String resData = HttpClientHelper.httpClientPost(YOU_FU_URL, map, CHARSET_UTF_8);
        System.out.println("http响应结果：" + resData);

        ResponseMessage responseMessage = JsonUtils.fromJson(resData, ResponseMessage.class);
        boolean verify = RSAUtils.verify(responseMessage.getResData(), responseMessage.getSign(), YOU_FU_PUBLIC_KEY);
        System.out.println("验签结果:" + verify);

        byte[] base64bs = Base64Utils.decode(responseMessage.getResData());
        byte[] debs = DESUtils.decrypt(base64bs, INTER_KEY);
        String detailData = new String(debs, CHARSET_UTF_8);
        System.out.println("个人账户余额查询结果：" + detailData);
        BalanceQueryResult balanceQueryResult=JsonUtils.fromJson(detailData, BalanceQueryResult.class);
        System.out.println("个人账户余额查询结束：" + balanceQueryResult);

    }

    /**
     * 组装请求json
     *
     * @return
     */
    private static String toJson() {
        BalanceQuery balanceQuery = new BalanceQuery();
        List<BalanceQuery.BalanceQueryItems> balanceQueryItemsList = new ArrayList<>();
        BalanceQuery.BalanceQueryItems balanceQueryItems = new BalanceQuery.BalanceQueryItems();
        balanceQueryItems.setName("wzk");
        balanceQueryItems.setIdCard("130823199407017879");
        balanceQueryItems.setLevyId(30481L);
        balanceQueryItemsList.add(balanceQueryItems);
        balanceQuery.setBalanceQueryItems(balanceQueryItemsList);
        return JSON.toJSONString(balanceQuery);
    }
}

